<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\duty\controller;

use app\common\controller\Admin;
use app\duty\controller\api\Event as EventApi;
use app\plan\controller\api\Plan as PlanApi;
use app\plan\controller\api\Record as RecordApi;
use app\admin\controller\ajax\Library as Ajax;
use app\plan\controller\api\Record;

/**
 * 事件管理
 * @package app\Event
 */
class Event  extends Admin

{

    protected $api;

    public function _initialize()
    {
        parent::_initialize();
        $this->api = new EventApi();
        $this->plan= new PlanApi();
        $this->record= new RecordApi();
        $this->ajax= new Ajax();
    }
    /**
     * 首页
     *
     */
    public function index() {
        $eventList=collection(model('events')->where(array('eventType'=>1))->with('user,enterprise,hazard,plan')->order('id desc')->limit(2)->select());
        $this->assign('list',$eventList);
        $userid=session("user_auth.id");
        $this->assign('userid',$userid);
        $type=$this->ajax->getplanType()->getData();
        $this->assign('type',$type);
        $eventtype=$this->ajax->getStatusType()->getData();
        $this->assign('eventtype',$eventtype);
        $enterpriseList = model('basic/Enterprise')->column('fullName','id');
        $this->assign('enterpriseList',$enterpriseList);
        $startTime=time();
        $this->assign('startTime',$startTime);
        $userList = model('common/user')->column('userName','id');
        $this->assign('userList',$userList);

        return $this->fetch();
    }

    /**
     * 事件
     */
    public function search($start=null,$end=null,$keyword=null) {
        if(IS_AJAX){
            $eventList = $this->api->search(input('start'),input('end'),input('keyword'));
            return $eventList;
        }
    }

    /**
     * 事件详情
     */
    public function detail($eventId) {
        $eventList = $this->api->eventList(input('eventId'))->getData();

        $this->assign('info',$eventList['data'][0]);
        $type=$this->ajax->getplanType()->getData();
        $this->assign('type',$type);
        $userList = model('common/user')->column('userName','id');
        $this->assign('userList',$userList);
        $enterpriseList = model('basic/Enterprise')->column('fullName','id');
        $this->assign('enterpriseList',$enterpriseList);
        $eventtype=$this->ajax->getStatusType()->getData();
        $this->assign('status',$eventtype);

                if(IS_POST){
                    $data = input('post.');

                    return $this->api->edit($data,$data['id']);
                 }


        return $this->fetch();
    }



    /**
     * 添加事件
     *
     */
    public function add() {
        if(IS_POST){

                $data = input('post.');
                $data['eventStatus']=1;
                return $this->api->add($data);
            }

        }


    /**
     * 修改事件
     *
     */
//    public function edit($eventId = null) {
//
//        if(IS_POST){
//
//            return $this->api->edit($data,$data['id']);
//        }
//
//        $userid=session("user_auth.id");
//        $this->assign('userid',$userid);
//        $type=$this->ajax->getplanType()->getData();
//        $this->assign('type',$type);
//        $eventtype=$this->ajax->getStatusType()->getData();
//        $this->assign('eventtype',$eventtype);
//        $enterpriseList = model('basic/Enterprise')->column('fullName','id');
//        $this->assign('enterpriseList',$enterpriseList);
//        $startTime=time();
//        $this->assign('startTime',$startTime);
//        $userList = model('common/user')->column('userName','id');
//        $this->assign('userList',$userList);
//        $eventList = $this->api->eventList($eventId)->getData();
//        $this->assign('info',$eventList['data'][0]);
//        return $this->fetch();
//    }

    /**
     * 删除事件
     */
//    public function del($eventId = null) {
//        if(IS_AJAX){
//            return $this->api->del($eventId);
//        }
//    }



    /**
     * 预案
     */
    public function plan($eventId = null) {
        if (IS_POST) {
            $pid=input('pid');
            $plan=model('Plan/plan')->with('scripts')->where(array('id'=>$pid))->select();
            return json(['data'=>$plan[0]]);
        }

        $reservePlanId=input('reservePlanId');
        if (!empty($reservePlanId)) {
            $this->assign('reservePlanId',$reservePlanId);
        }

        $planlist=model('Plan/plan')->where(array('natureId'=>1,'formId'=>2))->select();
        $this->assign('eventId',$eventId);
        $this->assign('planlist',$planlist);
        return $this->fetch();
    }

    /**
     * 设置预案
     */
    public function setPlan($id,$reservePlanId){
        if (empty($id)&&empty($reservePlanId)) {
            return false;
        }
        $result=model('events')->where(array('id'=>$id))->update(['reservePlanId'=>$reservePlanId]);


        $record=[];
        $record['eventId']=$id;
        $record['reservePlanId']=$reservePlanId;
        $record['typeId']=1;
        $record['execTime']=time();
        $record['operationId']=session('user_auth.id');
        $record['updateTime']=time();
        $res=$this->record->add($record)->getData();


        //救援行动表id

        $rid=$res['recordId'];

        //把预案表中的步骤插入行动明细中
        $scripts=model('plan/script')->where(array('reservePlanId'=>$reservePlanId))->select();
        $model=model('plan/recorddetail');
        foreach ($scripts as $key => $value) {
            $data=[];
            $data['rescueExecId']=$rid;
            $data['actionId']=$value->actionIdNum;
            $data['chargePerson']=$value['chargePerson'];
            $data['actionStatus']=0;
            $data['implementStatus']=0;
            $data['stepDesc']=$value['stepDesc'];
            $data['stepIndex']=$value['stepIndex'];
            $arr[$key]=$data;
        }
        $model->saveAll($arr);

        if ($result&&$res) {
            return json(['code'=>1,'msg'=>'预案选择成功','url'=>url('duty/panorama/index'),'layer'=>true]);
        }else{
            return json(['code'=>0,'msg'=>'预案启动失败,请检查']);
        }
   }


    /**
     * 查看位置
     */
    public function position($dangerSourceId){

        $position=model('basic/dangersource')->where(array('id'=>$dangerSourceId))->field('latitudeLongitude')->find();

        return json(['code'=>1,'position'=>$position['latitudeLongitude']]);

    }

    public function more()
    {
        $page = input('page');
        if (IS_POST) {

            // 获取数据库中所有数据的总数
            $count = model('duty/events')->with('user,enterprise,hazard,plan')->where(array('eventType' => 1))->count();
            // 获取前段接受到的总数
            $getCount = $page;

            if ($getCount <= $count) {
                $code = 1;
                $eventList = collection(model('events')->with('user,enterprise,hazard,plan')->where(array('eventType' => 1))->order('startTime desc')->limit(2 * $page)->select());
            } else {
                $code = 1;
                $eventList = collection(model('events')->with('user,enterprise,hazard,plan')->where(array('eventType' => 1))->order('startTime desc')->limit(2 * $page)->select());
            }

            return json(['code' => $code, 'data' => $eventList]);
        }
    }


        /**
         * 结束演练
         */
        public function over($id=null){


            $result=model('events')->where(array('id'=>$id))->update(['eventStatus'=>2]);
            if ($result) {
                return json(['code'=>1,'msg'=>'已结束']);
            }else{
                return json(['code'=>0,'msg'=>'有问题,请检查']);
            }
        }


}



